﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLTH_BUSINESS;
using QLTH_DTO;
using unitlControl;
namespace QuanLy_THPT_S4
{
    public partial class frmGiaoVienUPDATE : Form
    {
        private readonly clsDanTocBU dtBU;
        private readonly clsHocViBU hvBU;
        private readonly clsTonGiaoBU tgBU;
        private readonly clsGiaoVienDTO gvDTO;
        private readonly clsGIaoVienBU gvBU;
        private readonly clsTCMBU tcmBU;

        private String _magv;

        public String Magv
        {
            get { return _magv; }
            set { _magv = value; }
        }

      
        public frmGiaoVienUPDATE()
        {
            InitializeComponent();
            dtBU = new clsDanTocBU();
            tgBU = new clsTonGiaoBU();
            hvBU = new clsHocViBU();
            gvDTO = new clsGiaoVienDTO();
            gvBU = new clsGIaoVienBU();
            tcmBU = new clsTCMBU();
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmGiaoVienUPDATE_Load(object sender, EventArgs e)
        {
            
            radioGroup1.SelectedIndex = 0;
            ComboUtils.SetDataSuorce(cmbDT, dtBU.DanhSachDanToc(), "MADT", "TENDANTOC", 0);
            ComboUtils.SetDataSuorce(cmbHV, hvBU.DanhSachHocVi(), "MAHV", "TENHOCVI", 0);
            ComboUtils.SetDataSuorce(cmbTG, tgBU.DanhSachTonGiao(), "MATG", "TENTONGIAO", 0);
            ComboUtils.SetDataSuorce(cmbTCM, tcmBU.DanhSachTCM(), "MATCM", "TOCHUYENMON", 0);
            if (Magv == "" || Magv == null)
            {
                return;
            }
            else
            {
                txtMagv.Text = Magv;
                txtHoten.Text = gvBU.Lay_GV_theoma(Magv).Rows[0]["HOTEN"].ToString();
                dtNgaySinh.EditValue = gvBU.Lay_GV_theoma(Magv).Rows[0]["NGAYSINH"];
                txtNoisinh.Text = gvBU.Lay_GV_theoma(Magv).Rows[0]["NOISINH"].ToString();
                txtDiachi.Text = gvBU.Lay_GV_theoma(Magv).Rows[0]["DIACHI"].ToString();
                txtSDT.Text = gvBU.Lay_GV_theoma(Magv).Rows[0]["SDT"].ToString();
                ComboUtils.SelectedItem(cmbDT, gvBU.Lay_GV_theoma(Magv).Rows[0]["DANTOC"].ToString());
                ComboUtils.SelectedItem(cmbHV, gvBU.Lay_GV_theoma(Magv).Rows[0]["HOCVI"].ToString());
                ComboUtils.SelectedItem(cmbTG, gvBU.Lay_GV_theoma(Magv).Rows[0]["TONGIAO"].ToString());
                ComboUtils.SelectedItem(cmbTCM, gvBU.Lay_GV_theoma(Magv).Rows[0]["TOCHUYENMON"].ToString());
                if (gvBU.Lay_GV_theoma(Magv).Rows[0]["PHAI"].ToString() == "Nam")
                {
                    radioGroup1.SelectedIndex = 0;
                }
                else
                {
                    radioGroup1.SelectedIndex = 1;
                }
          
            }
            
        }

        private void txtHoten_EditValueChanged(object sender, EventArgs e)
        {
            
        }

      
        private void btnNhapLai_Click(object sender, EventArgs e)
        {
            txtMagv.Text = "";
            txtHoten.Text = "";
            txtHoten.Focus();
            ComboUtils.SetDataSuorce(cmbDT, dtBU.DanhSachDanToc(), "MADT", "TENDANTOC", 0);
            ComboUtils.SetDataSuorce(cmbHV, hvBU.DanhSachHocVi(), "MAHV", "TENHOCVI", 0);
            ComboUtils.SetDataSuorce(cmbTG, tgBU.DanhSachTonGiao(), "MATG", "TENTONGIAO", 0);
            ComboUtils.SetDataSuorce(cmbTCM, tcmBU.DanhSachTCM(),"MATCM","TOCHUYENMON",0);
            txtNoisinh.Text = "";
            txtDiachi.Text = "";
            txtSDT.Text = "";
          
        }

        private void btnLuuGV_Click(object sender, EventArgs e)
        {
           
            gvDTO.Holot = toancuc.Ho_Ten(txtHoten.Text)[0].Trim();
            gvDTO.Ten = toancuc.Ho_Ten(txtHoten.Text)[1].Trim();
            gvDTO.Phai = int.Parse(radioGroup1.EditValue.ToString());
            gvDTO.Hocvi.Mahv = ComboUtils.LayGT_Combobox(cmbHV);
            gvDTO.dantoc.Madt = ComboUtils.LayGT_Combobox(cmbDT);
            gvDTO.tongiao.Matg = ComboUtils.LayGT_Combobox(cmbTG);
            gvDTO.Tcm.Matcm = ComboUtils.LayGT_Combobox(cmbTCM);
            gvDTO.Noisinh = txtNoisinh.Text;
            gvDTO.Diachi = txtDiachi.Text;
            
            gvDTO.Ngaysinh = DateTime.Parse(dtNgaySinh.EditValue.ToString());

            if (txtMagv.Text == "" || txtMagv.Text == null)
            {
                gvDTO.Magv = toancuc.Tao_ma(gvBU.macuoi(), "GV", 3);
                gvBU.Them_giaovien(gvDTO);
               
                btnNhapLai_Click(sender, e);
                
            }
            else
            {
                gvDTO.Magv = txtMagv.Text;
                gvBU.Capnhat_giaovien(gvDTO);
            }
                     
        }

        private void frmGiaoVienUPDATE_FormClosing(object sender, FormClosingEventArgs e)
        {
      
        }
     
    }
}
